package com.nb.protocol.db;

import com.nb.protocol.protocol.MessageHolder;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @function:
 * @author: hjw
 * @date: 2018/1/7
 * @company: Sinoregal
 * @copyright: 2018 www.sinoregal.cn. All rights reserved.
 */
public class VbatDao {
    private static final Logger logger = Logger.getLogger(VbatDao.class);
    public static void reportData(MessageHolder messageHolder) throws SQLException {
        String sql = "INSERT INTO sensor_vbat (sn,create_date,VALUE) VALUES (?,?,?) ";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = DbCtrl.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,messageHolder.getCpuId());
            pstmt.setTimestamp(2,new Timestamp(new Date().getTime()));
            pstmt.setInt(3,messageHolder.getBody()[0]);
            pstmt.execute();
            //connection.commit();
        }catch (SQLException e){
            logger.error("写入电池数据出现错误:"+e.getMessage());
            throw new SQLException(e);
        }finally {
            try {
                DbCtrl.close(connection,pstmt);
            } catch (SQLException e) {
            }
        }

    }
}
